package com.java.example.ui.activity

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.os.Handler
import android.text.TextUtils
import android.view.View
import androidx.activity.ComponentActivity
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.withResumed
import cn.jiguang.api.utils.JCollectionAuth
import com.blankj.utilcode.util.SPUtils
import com.java.example.R
import com.java.example.app.AccountManager
import com.java.example.app.BleDeviceManager
import com.java.example.app.Constant
import com.java.example.app.MyApplication
import com.java.example.bean.UserProfile
import com.java.example.databinding.ActivitySplashBinding
import com.java.example.ui.dialog.AgreeDialog
import com.java.example.util.AccountUtil
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import java.text.SimpleDateFormat
import java.util.Date

@SuppressLint("CustomSplashScreen")
class SplashActivity : ComponentActivity() {
    private var time_count = 4
    private var agreeDialog: AgreeDialog? = null
    private lateinit var binding: ActivitySplashBinding
    private val mHandler = Handler()
    private val runnable: Runnable = object : Runnable {
        override fun run() {
            if (time_count == 1) {
                binding.btnJump.isEnabled=false
                doNext()
            } else {
                time_count--
                binding.btnJump.text = "跳过 $time_count"
                mHandler.postDelayed(this, 1000)
            }
        }
    }

    override fun onDestroy() {
        mHandler.removeCallbacks(runnable)
        super.onDestroy()
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = DataBindingUtil.setContentView(this, R.layout.activity_splash)

        SPUtils.getInstance().put(Constant.IS_CONNECT_BLUE, false) //蓝牙状态重制
        if (BleDeviceManager.getInstance().currentBleDevice != null) {
            BleDeviceManager.getInstance().disconnect()
        }

        val isFirst = SPUtils.getInstance().getBoolean(Constant.IS_FIRST_SHOW_AGREE, true)
        if (isFirst) {
            binding.btnJump.visibility = View.GONE
            agreeDialog = AgreeDialog.create(this)
            agreeDialog!!.setOnActionListener(object : AgreeDialog.OnActionListener {
                override fun agree() {
                    SPUtils.getInstance().put(Constant.IS_FIRST_SHOW_AGREE, false)
                    JCollectionAuth.setAuth(this@SplashActivity, false)
                    MyApplication.sInstance.initSDK()
                    CoroutineScope(Dispatchers.IO).launch {
                        delay(1000)
                        withResumed {
                            toLogin()
                        }
                    }
                }
            })
            agreeDialog!!.showDialog(this)
        } else {
            JCollectionAuth.setAuth(this@SplashActivity, false)
            if (isTodayFirst()) {
                binding.btnJump.visibility = View.VISIBLE
                mHandler.postDelayed(runnable, 500)
            } else {
                CoroutineScope(Dispatchers.IO).launch {
                    delay(1500)
                    withResumed {
                        doNext()
                    }
                }
            }
        }
        binding.btnJump.setOnClickListener {
            doNext()
        }
    }

    fun doNext() {
        if (SPUtils.getInstance().getBoolean(Constant.IS_LOGIN_STATE)) {
            AccountManager.setUserProfile(AccountUtil.getObject<UserProfile>(this@SplashActivity, Constant.USER_CACHE))
            toMain()
        } else {
            toLogin()
        }
    }

    private fun toLogin() {
        val intent = Intent(this@SplashActivity, LoginActivity::class.java)
        startActivity(intent)
        finish()
    }

    private fun toMain() {
        val intent = Intent(this@SplashActivity, MainActivity::class.java)
        startActivity(intent)
        finish()
    }

    private fun isTodayFirst(): Boolean {
        try {
            val saveTime = SPUtils.getInstance().getString(Constant.IS_FIRST_SHOW_TODAY)
            val todayTime = SimpleDateFormat("yyyy-MM-dd").format(Date())
            if (!TextUtils.isEmpty(saveTime) && !TextUtils.isEmpty(todayTime) && saveTime == todayTime) {
                return false
            } else SPUtils.getInstance().put(Constant.IS_FIRST_SHOW_TODAY, todayTime)
        } catch (e: Exception) {
            e.printStackTrace()
            return true
        }
        return true
    }
}